package pathways.marvin_test;

import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;


/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author Jia An Chen
 * @version 1.0
 */

public class AtomTraceEditorTableModel extends AbstractTableModel {
  public static final ColumnData m_columns[] = {
    new ColumnData( "From", 40, JLabel.CENTER ),
    new ColumnData( "Product", 80, JLabel.CENTER ),
    new ColumnData( "To", 100, JLabel.LEFT )
  };

  public static final int COL_FROM = 0;
  public static final int COL_PROD = 1;
  public static final int COL_TO = 2;

  private AtomTraceFrame theFrame;
  private Vector m_vector;
//  private int[] toColumnChoices;

  public AtomTraceEditorTableModel(int from, AtomTraceFrame theFrame) {
    this.theFrame = theFrame;
    CTracePath data;
    m_vector = new Vector(from);

    for (int i = 0; i < from; i++) {
      data = new CTracePath(String.valueOf(i+1), " ", " ");
      m_vector.add(i, data);
    }

  }

  public int getColumnCount() {
    return m_columns.length;
  }

  public String getColumnName(int column) {
    return m_columns[column].m_title;
  }

  public boolean isCellEditable(int nRow, int nCol) {
    if (nCol == COL_PROD) {
      return true;
    }
    else if (nCol == COL_TO) {
      CTracePath row = (CTracePath)m_vector.elementAt(nRow);
      return !row.m_product.equals(" ");
    }
    else {
      return false;
    }
//    return (nCol == COL_TO || nCol == COL_PROD);
  }

  public Object getValueAt(int nRow, int nCol) {
    if (nRow < 0 || nRow>=getRowCount())
      return "";

    CTracePath row = (CTracePath)m_vector.elementAt(nRow);
    switch (nCol) {
      case COL_FROM: return row.m_from;
      case COL_PROD: return row.m_product;
      case COL_TO: return row.m_to;
    }

    return "";
  }

  public int getRowCount() {
    return m_vector==null ? 0 : m_vector.size();
  }

  public void setValueAt(Object value, int nRow, int nCol) {
    if (nRow < 0 || nRow>=getRowCount())
      return;

    CTracePath row = (CTracePath)m_vector.elementAt(nRow);
    String svalue = (String)value;

    switch (nCol) {
      case COL_FROM:
        row.m_from = svalue;
        break;
      case COL_PROD:
        if (svalue.equals(" ")) {
          this.theFrame.setAtomChoiceEmpty(nRow);
        }
        else if (this.theFrame.isAlreadySelected(this, nRow, svalue, row.m_to)) {
          JOptionPane.showMessageDialog(this.theFrame, "The atom for this product has been selected already.", "Data Update Dialog", JOptionPane.INFORMATION_MESSAGE);
          this.theFrame.colorAtoms();
          return;
        }
        row.m_product = svalue;
        break;
      case COL_TO:
        if (!svalue.equals(" ")) {
          if (row.m_product.equals(" ")) {
            JOptionPane.showMessageDialog(this.theFrame, "Please select the product molecule first.", "Alert", JOptionPane.ERROR_MESSAGE);
            return;
          }

          if (this.theFrame.isAlreadySelected(this, nRow, row.m_product, svalue)) {
            JOptionPane.showMessageDialog(this.theFrame, "The atom has been selected already.", "Data Update Dialog", JOptionPane.INFORMATION_MESSAGE);
            return;
          }
        }

        row.m_to = svalue;
        break;
    }

    this.theFrame.colorAtoms();
//    try {
//      this.theFrame.colorAtoms(this, nRow, row.m_product, Integer.parseInt(row.m_to)-1);
//    }
//    catch (NumberFormatException nfe) {
//      this.theFrame.colorAtoms(this, nRow, row.m_product, -1);
//    }
  }

  public boolean isAlreadySelected(AtomTraceEditorTableModel model, int nRow, String product, String to) {
    if (to.equals(" "))
      return false;

    if (model == this) {
      for (int i = 0; i < this.m_vector.size(); i++) {
        if (i != nRow
            && product.equals(((CTracePath)m_vector.elementAt(i)).m_product)
            && to.equals(((CTracePath)m_vector.elementAt(i)).m_to))
          return true;
      }
    }
    else {
      for (int i = 0; i < this.m_vector.size(); i++) {
        if (product.equals(((CTracePath)m_vector.elementAt(i)).m_product)
            && String.valueOf(to).equals(((CTracePath)m_vector.elementAt(i)).m_to)) {
          return true;
        }
      }
    }

    return false;
  }

  public void setTrace(int nRow, String product, String to) {
    if (nRow < 0 || nRow>=getRowCount())
      return;

    CTracePath row = (CTracePath)m_vector.elementAt(nRow);
    row.m_product = product;
    row.m_to = to;

    this.fireTableRowsUpdated(nRow, nRow);
  }

  public Vector getTracePaths() {
    Vector tracePaths = new Vector();

    for (int i = 0; i < m_vector.size(); i++) {
      CTracePath path = (CTracePath)m_vector.elementAt(i);
      if (!path.m_to.equals(" ")) {
        tracePaths.add(path);
      }
    }
    return tracePaths;
  }
}

class ColumnData
{
  public String  m_title;
  int m_width;
  int m_alignment;

  public ColumnData(String title, int width, int alignment) {
    m_title = title;
    m_width = width;
    m_alignment = alignment;
  }
}



